In [ ]:
% reset -f
from __future__ import print_function
from __future__ import division
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# ! pip install torchvision

Using PyTorch


In [7]:
import torch
import sys
print('__Python VERSION:', sys.version)
print('__pyTorch VERSION:', torch.__version__)
print('__CUDA VERSION')
from subprocess import call
# call(["nvcc", "--version"]) does not work
! nvcc --version
print('__CUDNN VERSION:', torch.backends.cudnn.version())
print('__Number CUDA Devices:', torch.cuda.device_count())
print('__Devices')
# call(["nvidia-smi", "--format=csv", "--query-gpu=index,name,driver_version,memory.total,memory.used,memory.free"])
print('Active CUDA Device: GPU', torch.cuda.current_device())

print ('Available devices ', torch.cuda.device_count())
print ('Current cuda device ', torch.cuda.current_device())


__Python VERSION: 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
__pyTorch VERSION: 0.2.1+a4fc05a
__CUDA VERSION
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Mon_Jan__9_17:32:33_CST_2017
Cuda compilation tools, release 8.0, V8.0.60
__CUDNN VERSION: None
__Number CUDA Devices: 1
__Devices
Active CUDA Device: GPU 0
Available devices  1
Current cuda device  0

Trick


In [4]:
use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
Tensor = FloatTensor

Using PyCUDA


In [5]:
import pycuda
from pycuda import compiler
import pycuda.driver as drv

drv.init()
print("%d device(s) found." % drv.Device.count())
           
for ordinal in range(drv.Device.count()):
    dev = drv.Device(ordinal)
    print (ordinal, dev.name())


1 device(s) found.
0 GeForce GTX 1080

Alloocate a PyCUDA Tensor on the GPU

https://documen.tician.de/pycuda/array.html


In [6]:
from pycuda import gpuarray
from pycuda.curandom import rand as curand
 # -- initialize the device
import pycuda.autoinit

height = 100
width = 200
X = curand((height, width), np.float32)
X.flags.c_contiguous 
print (type(X))


---------------------------------------------------------------------------
CompileError                              Traceback (most recent call last)
<ipython-input-6-831319b240cc> in <module>()
      6 height = 100
      7 width = 200
----> 8 X = curand((height, width), np.float32)
      9 X.flags.c_contiguous
     10 print (type(X))

C:\Anaconda3\lib\site-packages\pycuda\curandom.py in rand(shape, dtype, stream)
    208                 dest[i] = d*POW_2_M32;
    209             """,
--> 210             "md5_rng_float")
    211     elif dtype == np.float64:
    212         func = get_elwise_kernel(

C:\Anaconda3\lib\site-packages\pycuda\elementwise.py in get_elwise_kernel(arguments, operation, name, keep, options, **kwargs)
    159     """
    160     func, arguments = get_elwise_kernel_and_types(
--> 161             arguments, operation, name, keep, options, **kwargs)
    162 
    163     return func

C:\Anaconda3\lib\site-packages\pycuda\elementwise.py in get_elwise_kernel_and_types(arguments, operation, name, keep, options, use_range, **kwargs)
    145 
    146     mod = module_builder(arguments, operation, name,
--> 147             keep, options, **kwargs)
    148 
    149     func = mod.get_function(name)

C:\Anaconda3\lib\site-packages\pycuda\elementwise.py in get_elwise_module(arguments, operation, name, keep, options, preamble, loop_prep, after_loop)
     73             "after_loop": after_loop,
     74             },
---> 75         options=options, keep=keep)
     76 
     77 

C:\Anaconda3\lib\site-packages\pycuda\compiler.py in __init__(self, source, nvcc, options, keep, no_extern_c, arch, code, cache_dir, include_dirs)
    289 
    290         cubin = compile(source, nvcc, options, keep, no_extern_c,
--> 291                 arch, code, cache_dir, include_dirs)
    292 
    293         from pycuda.driver import module_from_buffer

C:\Anaconda3\lib\site-packages\pycuda\compiler.py in compile(source, nvcc, options, keep, no_extern_c, arch, code, cache_dir, include_dirs, target)
    253         options.append("-I"+i)
    254 
--> 255     return compile_plain(source, options, keep, nvcc, cache_dir, target)
    256 
    257 class CudaModule(object):

C:\Anaconda3\lib\site-packages\pycuda\compiler.py in compile_plain(source, options, keep, nvcc, cache_dir, target)
     76 
     77         if '#include' in source:
---> 78             checksum.update(preprocess_source(source, options, nvcc).encode("utf-8"))
     79         else:
     80             checksum.update(source.encode("utf-8"))

C:\Anaconda3\lib\site-packages\pycuda\compiler.py in preprocess_source(source, options, nvcc)
     53         from pycuda.driver import CompileError
     54         raise CompileError("nvcc preprocessing of %s failed" % source_path,
---> 55                            cmdline, stderr=stderr)
     56 
     57     # sanity check

CompileError: nvcc preprocessing of C:\Users\gpu\AppData\Local\Temp\tmprlf28h46.cu failed
[command: nvcc --preprocess -arch sm_61 -m64 -Ic:\anaconda3\lib\site-packages\pycuda\cuda C:\Users\gpu\AppData\Local\Temp\tmprlf28h46.cu --compiler-options -EP]

In [ ]: